import Vue from 'vue'
import VueRouter from 'vue-router'
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const routes = [
  {
    path: "/login",
    component: () => import("@/views/login.vue")
  }
  , {
    path: "/singIn",
    component: () => import("@/views/singIn")
  }, {
    path: "/personal",
    component: () => import("@/views/personal")
  }, {
    path: "/edit_profile",
    component: () => import("@/views/edit_profile")
  }, {
    path: "/attention",
    component: () => import("@/views/attention")
  }, {
    path: "/index",
    component: () => import("@/views/index"),
    // 路由元信息
    meta: {
      curIndex: localStorage.getItem("token") ? 1 : 0
    }
  }, {
    path: "/newsDetails",
    component: () => import("@/views/newsDetails")
  }, {
    path: "/comment",
    component: () => import("@/views/comment")
  }, {
    path: "/category",
    component: () => import("@/views/category")
  }, {
    path: "/publish",
    component: () => import("@/views/publish")
  }, {
    path: "/search",
    component: () => import("@/views/search")
  }, {
    path: "/favorite",
    component: () => import("@/views/favorite")
  }, {
    path: "/allSingle",
    component: () => import("@/components/allSingle")
  }, {
    path: "/",
    redirect: "/login"
  }
]

const router = new VueRouter({
  routes
})
let arr = ['/login', '/singIn', '/index', '/newsDetails']
router.beforeEach((to, from, next) => {
  if (arr.indexOf(to.path) !== -1) {
    next()
  } else {
    if (localStorage.getItem("token")) {
      next()
    } else {
      next("/login")
    }
  }

})
export default router
